const reqlib = require('app-root-path').require
const localConfig = reqlib('/config')
const axios = require('axios')
const qs = require('qs')

const env = process.env.NODE_ENV
const isDev = env === 'dev'

const stockAddress = localConfig.stockDataAddress
const binanceAddress = localConfig.binanceAddress

function makeStockUrl (path) {
  return `${stockAddress}/${path}`
}

function makeBinancekUrl (path) {
  return `${binanceAddress}/${path}`
}

function axiosBinance (url, query, options) {
  let queryString = ''
  if (query) {
    query.timestamp = new Date().getTime()
    queryString = qs.stringify(query)
  } else {
    queryString = qs.stringify({ timestamp: new Date().getTime() })
  }
  let start = new Date().getTime()
  return axios.get(makeBinancekUrl(url + (queryString ? '?' + queryString : '')), options).then((data) => {
    if (isDev) {
      console.log(`币安耗时:${new Date().getTime() - start} (${url})`)
    }
    return data.data
  })
}

// 获取基金api的query
function axiosGetStock (url, query, options) {
  let queryString = ''
  if (query) {
    query.timestamp = new Date().getTime()
    queryString = qs.stringify(query)
  } else {
    queryString = qs.stringify({ timestamp: new Date().getTime() })
  }
  return axios.get(makeStockUrl(url + (queryString ? '?' + queryString : '')), options).then(data => data.data)
}

exports.getStockRate = function (data) {
  return axiosGetStock('stockData/getStockRate', {
    start: data.start,
    way: data.way || '腾讯',
    code: data.code
  })
}

// 获取交易日列表
exports.getTradingDays = function (data) {
  return axiosGetStock('stockData/getTradingDays', {
    days: data.days || 1,
    way: data.way || '腾讯'
  })
}

// 获取上一交易日
exports.getLastTradingDay = function (data) {
  data = data || { way: '腾讯' }
  return axiosGetStock('stockData/getLastTradingDay', {
    way: data.way
  })
}

exports.getStockTodayTenxun = function (data) {
  return axiosGetStock('stockData/getStockTodayTenxun', {
    code: data.code
  })
}

exports.getStockAllTenxun = function (data) {
  return axiosGetStock('stockData/getStockAllTenxun', {
    code: data.code,
    days: data.days || 1
  })
}

exports.getStockTodayDongfang = function (data) {
  return axiosGetStock('stockData/getStockTodayDongfang', {
    code: data.code
  })
}

exports.getStockAllDongfang = function (data) {
  return axiosGetStock('stockData/getStockAllDongfang', {
    code: data.code,
    days: data.days || 1
  })
}

exports.updateXueQiuCookie = function () {
  return axiosGetStock('stockData/updateXueQiuCookie')
}

exports.getStockTodayXueqiu = function (data) {
  return axiosGetStock('stockData/getStockTodayXueqiu', {
    code: data.code
  })
}

exports.getStockAllXueqiu = function (data) {
  return axiosGetStock('stockData/getStockAllXueqiu', {
    code: data.code,
    days: data.days || 1
  })
}

exports.getBtbKlines = function () {
  return axiosGetStock('stockData/getBtbKlines')
}

exports.getETHKlines = function () {
  return axiosGetStock('stockData/getETHKlines')
}

exports.getDOGEKlines = function () {
  return axiosGetStock('stockData/getDOGEKlines')
}

exports.getBNBKlines = function () {
  return axiosGetStock('stockData/getBNBKlines')
}

exports.getBIKlines = function (data) {
  return axiosGetStock('stockData/getBIKlines', {
    name: data.name
  })
}

exports.getBIHTKlines = function (data) {
  return axiosGetStock('stockData/getBIHTKlines', {
    name: data.name
  })
}

// 执行
exports.binance = function (url, query, options) {
  return axiosBinance(url, query, options)
}

exports.getBIBTKlines = function (data) {
  return axiosBinance('kLinesAll', {
    key: `${data.name}USDT`,
    interval: `${data.interval}`,
    count: data.count || 500
  }).then((res) => {
    return res.data || []
  })
}

exports.getFuturesKLinesAll = function (data) {
  return axiosBinance('futuresKLinesAll', {
    key: `${data.name}USDT`,
    interval: `${data.interval}`,
    count: data.count || 500
  }).then((res) => {
    return res.data || []
  })
}
